<template>
  <div>
    <div style="margin-bottom:20px">
        <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>
    </div>

  <el-col span="4">
    <el-input
      placeholder="请输入内容"
      v-model.trim="searchMsg"
      v-on:input="fetchUser">
      <i slot="prefix" class="el-input__icon el-icon-search" id="search_input"></i>
    </el-input>
  </el-col>

  <el-table
    stripe
    v-loading="loading"
    ref="multipleTable"
    :data="users"
    tooltip-effect="dark"
    style="width: 100%"
    height="600"
    @selection-change="handleSelectionChange"
    >
    <el-table-column
      type="selection"
      width="55">
    </el-table-column>
    <el-table-column
      prop="userid"
      label="id"
      sortable
      width="120">
      <!-- <template slot-scope="scope">{{ scope.row.id }}</template> -->
    </el-table-column>
    <el-table-column
      prop="username"
      label="用户名"
      sortable
      width="120">
    </el-table-column>
    <el-table-column
      prop="password"
      label="密码"
      show-overflow-tooltip>
    </el-table-column>
    <el-table-column
      prop="phone"
      label="电话"
       >
    </el-table-column>

    <el-table-column
      prop="address"
      label="地址"
       >
    </el-table-column>

    <el-table-column
      prop="point"
      label="积分"
       >
    </el-table-column>

    <el-table-column
      label="VIP"
       >
      <template slot-scope="scope">
         <el-switch
          v-model="scope.row.isVIP"
          active-color="#13ce66"
          inactive-color="#ff4949">
        </el-switch>
      </template>
    </el-table-column>

    <el-table-column
        fixed="right"
        label="操作"
        width="150">
        <template slot-scope="scope">
            <el-button @click="deleteUser(scope.row)" type="danger" size="mini" >删除</el-button>
            <el-button size="mini" @click="editButtonClick(scope.row)">编辑</el-button>
        </template>
    </el-table-column>
  </el-table>
<el-row :gutter="10" style="margin-top:20px">
    <el-col :span="2">
          <el-button type="danger" size="small" :disabled="multipleSelection.length==0? true:false" @click="deleteSelect()">删除所选</el-button>
    </el-col>
    <el-col :span="2">
      <el-dialog title="修改用户" :visible.sync="dialogFormVisible">
        <el-form :model="selectedUser">
          <el-form-item label="用户名" :label-width="formLabelWidth">
            <el-input v-model="selectedUser.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth">
            <el-input v-model="selectedUser.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话" :label-width="formLabelWidth">
            <el-input v-model="selectedUser.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="地址" :label-width="formLabelWidth">
            <el-input v-model="selectedUser.address" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="积分" :label-width="formLabelWidth">
            <el-input v-model="selectedUser.point" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false,form = {}">取 消</el-button>
          <el-button type="primary" @click="editUser()">确 定</el-button>
        </div>
      </el-dialog>

    </el-col>
</el-row>
<el-row>
  <div class="block">
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="10"
      layout="total, prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</el-row>



</div>
</template>

<script>

// 节流函数
const delay = (function() {
  let timer = 0;
  return function(callback, ms) {
    clearTimeout(timer);
    timer = setTimeout(callback, ms);
  };
})();

export default {
    name:"userList",
    data(){
        return {
            loading:true,
            dialogFormVisible: false,
            multipleSelection: [],
            users:[],
            selectedUser:{},
            form:{},
            searchMsg:'',
            currentPage:1,
            total:0
        }
    },

    created(){
        this.getUserList()
    },

    methods:{
        getUserList(){
          this.loading = true
            this.$axios.get('/getUserList',{
                params:{
                    currentPage:this.currentPage,
                    searchMsg:this.searchMsg,
                }
            }).then(resp =>{
                if(resp && resp.status === 200){
                    console.log(resp.data)
                    this.users = resp.data.data
                    this.total = resp.data.total
                    this.loading = false
                }
            })
        },
        
        fetchUser(){
          this.currentPage = 1
          clearTimeout(this.timer);
          this.timer=setTimeout(()=>{
            this.getUserList()
          },800);
        },

        
        handleCurrentChange(val) {
            if(this.currentPage!=val){
              this.currentPage = val;
              this.getUserList()
            }
        },

        handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log(this.multipleSelection);
        },

        deleteUser(user){
          this.$axios.get('/deleteUser?id='+user.userid).then(resp=>{
            if(resp.status==200){
              this.$message({message: '删除成功', type: 'success'});
              this.getUserList()
            }
          })
        },
        editButtonClick(user){
          this.selectedUser = user
          this.dialogFormVisible = true
        },
        editUser(){
          this.$axios.post('/editUser',this.selectedUser).then(resp=>{
            if(resp.status==200){
              this.getUserList()
            }
          })
        },
        deleteSelect(){
          for(let item of this.multipleSelection){
            this.deleteUser(item)
          }
        }
    }
}
</script>

<style>

</style>